package com.qf.Service.Impl;/*
 *
 */

import com.qf.Dao.Impl.UserDaoImpl;
import com.qf.Dao.UserDao;
import com.qf.Domain.Address;
import com.qf.Domain.User;
import com.qf.Service.UserService;
import com.qf.Utils.EmailUtils;
import com.qf.Utils.Md5Utils;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();
    @Override
    //添加用户
    public void insert(User user) {
        boolean b = userDao.select(user.getUsername());
        if (b){
            throw new RuntimeException("用户名已存在");
        }
        //密码加密
        String s = Md5Utils.md5(user.getPassword());
        user.setPassword(s);
        userDao.insert(user);
        EmailUtils.sendEmail(user);
    }

    @Override
    public User select(String username, String password) {
        Md5Utils.md5(password);
        User user = userDao.select(username, password);
        if (user!=null&&user.getFlag()!=1){
            throw new RuntimeException("账号失效或未激活");
        }
            return user;
    }

    @Override
    public boolean select(String username) {
        return userDao.select(username);
    }

    @Override
    public List<Address> findAddress(Integer uid) {
        return userDao.selectAddress(uid);
    }

    @Override
    public void addAddress(Address address) {
        userDao.addAddress(address);
    }

    @Override
    public void updateAddressStatus(Integer uid,Integer aid) {
        userDao.updateAddressStatus(uid,aid);
    }

    @Override
    public void updateAddress(Address address) {
        userDao.updateAddress(address);
    }

    @Override
    public void delete(int aid) {
        userDao.delete(aid);
    }
}
